<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\users;

use app\common\dao\BaseDao;
use app\common\model\users\UsersOauthModel;
use think\db\BaseQuery;

class UsersOauthDao extends BaseDao
{

    /**
     * @return UsersOauthModel
     */
    protected function getModel(): string
    {
        return UsersOauthModel::class;
    }

    public function search(array $where = [], int $propertyId = null): BaseQuery
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', (int)$propertyId);
            })
            ->when(isset($where['oauth_type']) && $where['oauth_type'] !== '', function ($query) use ($where) {
                $query->where('oauth_type', $where['oauth_type']);
            })
            ->when(isset($where['user_id']) && $where['user_id'] !== '', function ($query) use ($where) {
                $query->where('user_id', (int)$where['user_id']);
            });
    }

    /**
     * 根据openid查询信息
     * @param string $openId
     * @param string $oauthType
     * @param int|null $propertyId
     * @return array|mixed|BaseQuery|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-01-24 22:10
     */
    public function getByOpenId(string $openId, string $oauthType = '', ?int $propertyId = null)
    {
        $query = $this->search([
            'oauth_type' => $oauthType
        ], $propertyId);

        $info = $query->where('openid', $openId)->find();

        return $info;
    }

    /**
     * 获取用户第三方信息ID
     * @param int $userId
     * @param string $oauthType
     * @return false|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-01-24 22:10
     */
    public function getUserOpenId(int $userId, string $oauthType)
    {
        $info = $this->search([
            'user_id' => $userId,
            'oauth_type' => $oauthType
        ])->find();
        if (!$info) {
            return false;
        }
        return $info->openid;
    }

    /**
     * 根据用户ID删除
     * @param int $userId
     * @return bool
     *
     * @author Amos
     * 2024-01-24 22:10
     */
    public function delByUserId(int $userId)
    {
        return $this->getSearch([
            'user_id' => $userId
        ])->delete();
    }

}
